package com.duguo.dynamicmvc.factory.utils.javassist;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.duguo.dynamicmvc.support.pathinfo.PathInfo;

public class ParameterUtils {

	public static String getRequestParam(CtMethod controllerMethod) throws Exception {
		return getParam(HttpServletRequest.class,controllerMethod);
	}


	public static String getResponseParam(CtMethod controllerMethod) throws Exception {
		return getParam(HttpServletResponse.class,controllerMethod);		
	}


	public static String getPathInfoParam(CtMethod controllerMethod) throws Exception {
		return getParam(PathInfo.class,controllerMethod);
	}
	
	public static String getParam(Class paramType,CtMethod controllerMethod) throws Exception {
		CtClass paramCtClass=ClassPool.getDefault().get(paramType.getName());
		int position=1;
		for(CtClass currentParam:controllerMethod.getParameterTypes()){
			if(paramCtClass==currentParam){
				return "$"+position;
			}
			position++;
		}
		throw new RuntimeException("Cannot find "+paramCtClass.getName()+" in the controllerMethod "+controllerMethod.getName()+"."+controllerMethod.getName());
	}
	

}
